package com.bolt.support.codegenerator.model;

import com.bolt.common.utils.StrUtil;
import com.bolt.support.codegenerator.util.JavaTypeResolver;

/**
 * 　DB表列结构
 * Created by Administrator on 2018/2/2.
 */
public class Column {

    //列名
    private String sqlName;
    //是否主键
    private boolean primaryKey;
    //是否外键
    private boolean foreignKey;
    //长度定义
    private int size;
    //小数位数
    private int decimalDigits;
    //是否可空
    private boolean nullable;
    //是否唯一
    private boolean unique;
    //是否索引列
    private boolean indexed;
    //是否自增长列
    private boolean autoincrement;
    //默认值
    private String defaultValue;
    //备注
    private String remarks;

    // 字段类型
    private int jdbcType;
    //字段类型名称
    private String jdbcTypeName;

    private String javaType;

    private String fullJavaType;

    //java属性名
    private String javaProperty;


    public Column(String sqlName) {
        this.sqlName = sqlName;
    }


    public Column(String sqlName, int jdbcType, String jdbcTypeName
            , int size, int decimalDigits, boolean isNullable,
                  String defaultValue, String remarks) {

        this.jdbcType = jdbcType;
        this.sqlName = sqlName;
        this.jdbcTypeName = jdbcTypeName;

        this.size = size;
        this.decimalDigits = decimalDigits;
        this.nullable = isNullable;

        this.defaultValue = defaultValue;
        this.remarks = remarks;
    }


    public String getSqlName() {
        return sqlName;
    }

    public void setSqlName(String sqlName) {
        this.sqlName = sqlName;
    }

    public boolean isPrimaryKey() {
        return primaryKey;
    }

    public void setPrimaryKey(boolean primaryKey) {
        this.primaryKey = primaryKey;
    }

    public boolean isForeignKey() {
        return foreignKey;
    }

    public void setForeignKey(boolean foreignKey) {
        this.foreignKey = foreignKey;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getDecimalDigits() {
        return decimalDigits;
    }

    public void setDecimalDigits(int decimalDigits) {
        this.decimalDigits = decimalDigits;
    }

    public boolean isNullable() {
        return nullable;
    }

    public void setNullable(boolean nullable) {
        this.nullable = nullable;
    }

    public boolean isUnique() {
        return unique;
    }

    public void setUnique(boolean unique) {
        this.unique = unique;
    }

    public boolean isIndexed() {
        return indexed;
    }

    public void setIndexed(boolean indexed) {
        this.indexed = indexed;
    }

    public boolean isAutoincrement() {
        return autoincrement;
    }

    public void setAutoincrement(boolean autoincrement) {
        this.autoincrement = autoincrement;
    }

    public String getDefaultValue() {
        return defaultValue == null ? "" : defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public int getJdbcType() {
        return jdbcType;
    }

    public void setJdbcType(int jdbcType) {
        this.jdbcType = jdbcType;
    }

    public String getJdbcTypeName() {
        return jdbcTypeName;
    }

    public void setJdbcTypeName(String jdbcTypeName) {
        this.jdbcTypeName = jdbcTypeName;
    }

    /**
     * 是否基本类型
     *
     * @return
     */
    public boolean isPrimitiveType() {
        // int, double, float, long, short, boolean, byte, char
        return JavaTypeResolver.isInteger(javaType) || JavaTypeResolver.isDouble(javaType)
                || JavaTypeResolver.isFloat(javaType) || JavaTypeResolver.isLong(javaType)
                || JavaTypeResolver.isShort(javaType) || JavaTypeResolver.isBoolean(javaType)
                || JavaTypeResolver.isByte(javaType);
    }

    public String getJavaPrimitiveType() {
        if (JavaTypeResolver.isInteger(javaType)) {
            return "int";
        } else if (JavaTypeResolver.isDouble(javaType)) {
            return "double";
        } else if (JavaTypeResolver.isFloat(javaType)) {
            return "float";
        } else if (JavaTypeResolver.isLong(javaType)) {
            return "long";
        } else if (JavaTypeResolver.isShort(javaType)) {
            return "short";
        } else if (JavaTypeResolver.isBoolean(javaType)) {
            return "boolean";
        } else if (JavaTypeResolver.isByte(javaType)) {
            return "byte";
        } else {
            return javaType;
        }
    }

    public boolean isString() {
        return JavaTypeResolver.isString(javaType);
    }

    public boolean isFloatNumber() {
        return JavaTypeResolver.isFloat(javaType) || JavaTypeResolver.isDouble(javaType)
                || JavaTypeResolver.isBigDecimal(javaType) || JavaTypeResolver.isBigInteger(javaType);
    }

    public boolean isIntegerNumber() {
        return JavaTypeResolver.isByte(javaType) || JavaTypeResolver.isShort(javaType)
                || JavaTypeResolver.isInteger(javaType) || JavaTypeResolver.isLong(javaType);
    }

    public boolean isBigDecimal() {
        return JavaTypeResolver.isBigDecimal(javaType);
    }

    public String getJavaType() {
        return javaType;
    }

    public void setJavaType(String javaType) {
        this.javaType = javaType;
    }

    public String getFullJavaType() {
        return fullJavaType;
    }

    public void setFullJavaType(String fullJavaType) {
        this.fullJavaType = fullJavaType;
    }

    public boolean isBoolean() {
        return JavaTypeResolver.isBoolean(javaType);
    }

    public boolean isDate() {
        return JavaTypeResolver.isDate(javaType);
    }

    public boolean isBLOB() {
        String typeName = getJdbcTypeName();
        boolean isBlob = "BINARY".equals(typeName);
        isBlob = isBlob || "BLOB".equals(typeName);
        isBlob = isBlob || "CLOB".equals(typeName);
        isBlob = isBlob || "LONGVARBINARY".equals(typeName);
        isBlob = isBlob || "LONGVARCHAR".equals(typeName);
        isBlob = isBlob || "VARBINARY".equals(typeName);
        return isBlob;
    }

    public void setJavaProperty(String javaProperty) {
        this.javaProperty = StrUtil.toCamelCase(javaProperty);
    }

    public String getJavaProperty() {
        return javaProperty;
    }


}
